/* eslint-disable max-classes-per-file */
/*
 * @Description:
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-08-15 13:02:23
 * @LastEditors: swc
 * @LastEditTime: 2025-02-14 13:39:31
 */
import { auditDefaultFields } from '../../config';
import { N_oSetupPanel } from '../../types';
import { T_compPanelConf } from '../compConfPanel';
import { T_toolbarLib } from '../compToolbar';
import { T_compBase } from './compBase';

type I_tableStyle = N_oSetupPanel.N_comps.I_tableStyle;
type I_compToolbar = N_oSetupPanel.N_comps.I_compToolbar;
type I_messageConf = N_oSetupPanel.N_comps.I_messageConf;
export interface I_formPanel {
  label: string;
  content: {
    name: string;
    formStyle: {
      // columnNum: number;
      // pixelWidth: number;
      // percentageWidth: number;
      isBottom?: boolean;
    };
    formColumns: Array<any>;
    uniqueIndexes: Array<any>;
    indexes: Array<any>;
    rules: Array<any>;
    dbType: string;
    // mainFun: string;
  };
}
export interface I_tablePanel {
  label: string;
  content: {
    name: string;
    dbType: string;
    tableStyle: I_tableStyle;
    tableColumns: Array<any>;
    uniqueIndexes: Array<any>;
    indexes: Array<any>;
    rules: Array<any>;
    action: object;
    headerGroup: Array<any>;
    charts: Array<any>;
    toolbar: I_compToolbar;
    tableDataSetting: {
      editable: boolean;
      autoload: boolean;
    };
  };
}
export class T_basePanelConf {
  name: string;

  comment: string;

  // mainFun: string;

  dbType: 'mysql' | 'mongo';

  isCreated: boolean;

  status: string;

  // tableStyle: I_tableStyle;

  bancodeScan: object; // 条码扫描

  // 表格样式
  toolbar: I_compToolbar;

  messageConfig: I_messageConf;

  selbill: Array<any>;

  queryLink: Array<any>;

  formPanels: Array<I_formPanel>;

  tablePanels: Array<I_tablePanel>;

  // 唯一索引
  indexes: Array<any>;

  uniqueIndexes: Array<any>;

  action: object;
}

export class T_basePanel extends T_compBase {
  private TtoolbarLib: T_toolbarLib = new T_toolbarLib();

  private TcompPanelConf: T_compPanelConf = new T_compPanelConf();

  public conf: T_basePanelConf = new T_basePanelConf();

  constructor() {
    super();
    this.conf = {
      name: '',
      comment: '',
      // mainFun: '',
      isCreated: false,
      status: '',
      dbType: N_oSetupPanel.N_comps.DB_TYPE.MYSQL,
      // tableStyle: {
      //   showHeader: true,
      //   showFooter: false,
      //   showPager: true,
      //   showToolbar: true,
      //   border: false,
      //   mode: N_oSetupPanel.N_comps.TABLESTYLE_MODE.MODE_NULL
      // },
      // 条码扫描
      bancodeScan: {
        enable: false,
        initSpace: 5,
        terminator: '',
        position: N_oSetupPanel.N_comps.POSITION.POSITION_TOP
      },
      toolbar: null,
      messageConfig: {
        custDefListener: {
          search: [],
          coordination: null
        },
        emitActions: {
          search: [],
          refresh: []
        },
        eventGroup: null
      },
      selbill: [],
      queryLink: [],
      indexes: [], // 索引
      uniqueIndexes: [],
      formPanels: [
        {
          label: 'main_info',
          content: {
            name: '',
            formStyle: {
              // columnNum: 0,
              // pixelWidth: 0,
              // percentageWidth: 0,
              isBottom: false
            },
            formColumns: [],
            uniqueIndexes: [],
            indexes: [],
            rules: [],
            dbType: ''
          }
        },
        {
          label: 'basic_info',
          content: {
            name: '',
            formStyle: {
              // columnNum: 0,
              // pixelWidth: 0,
              // percentageWidth: 0,
              isBottom: false
            },
            formColumns: [...this.getAuditDefaultFields()],
            uniqueIndexes: [],
            indexes: [],
            rules: [],
            dbType: ''
          }
        }
      ],
      tablePanels: [
        {
          label: '',
          content: {
            name: '',
            dbType: '',
            tableStyle: {
              showHeader: true,
              showFooter: true,
              // showPager: false,
              showToolbar: true,
              border: true,
              // eslint-disable-next-line no-use-before-define
              mode: N_oSetupPanel.N_comps.TABLESTYLE_MODE.MODE_NULL
            },
            tableColumns: [],
            uniqueIndexes: [],
            indexes: [],
            rules: [],
            action: {},
            headerGroup: [],
            charts: [],
            // toolbar: I_compToolbar;
            toolbar: {
              // 待做配置
              default: true,
              insert: true,
              delete: true,
              save: false,
              import: true,
              export: true,
              globalSearch: true,
              edit: true,
              print: true,
              refresh: false,
              addon: []
            },
            tableDataSetting: {
              editable: true,
              autoload: true
            }
          }
        }
      ],
      action: {}
    };
    this.conf.toolbar = this.TtoolbarLib.getCompToolBarSet(
      N_oSetupPanel.N_comps.COMP_NAME.BASE_PANEL
    );
  }

  public getAuditDefaultFields() {
    const fields = auditDefaultFields();
    fields.forEach(item => {
      item.isShow = true;
    });
    return fields;
  }

  public addSelbill(...p: any[]): void {
    this.conf.selbill.push(...p);
  }

  public addQueryLink(...p: any[]): void {
    this.conf.queryLink.push(...p);
  }

  public addUniqueIndexes(...p: any[]): void {
    this.conf.uniqueIndexes.push(...p);
  }

  public getConf(): T_basePanelConf {
    return this.conf;
  }

  public getComponentId(): N_oSetupPanel.N_comps.COMP_MODULE_ID {
    return N_oSetupPanel.N_comps.COMP_MODULE_ID.BASE_PANEL;
  }

  public GetCompPanelConf(): N_oSetupPanel.N_comps.I_setupPanel {
    return this.TcompPanelConf.getCompPanelSet(
      N_oSetupPanel.N_comps.COMP_NAME.BASE_PANEL
    );
  }

  public getPermissionList(
    config: T_basePanelConf
  ): Array<N_oSetupPanel.I_permission> {
    return [
      {
        type: 'button',
        area: 'toolbar',
        data: this.getToolbarKey(config)
      },

      {
        type: 'selbill',
        area: 'toolbar',
        data: this.getSelbillData(config)
      },
      {
        type: 'customButton',
        area: 'toolbar',
        data: this.getPmsAddonData(config)
      },
      {
        type: 'columns',
        area: 'form',
        data: this.getPmsForm(config)
      },
      {
        type: 'columns',
        area: 'table',
        data: this.getPmsColumn(config)
      }
    ];
  }

  private getToolbarKey(config: any): Array<string> {
    if (config?.tableColumns) {
      return ['insert', 'delete', 'edit'];
    }
    if (config?.formPanels) {
      return Object.keys(this.conf.toolbar);
    }
    return [];
  }

  private getSelbillData(config: T_basePanelConf): Array<string> {
    return [];
  }

  private getPmsColumn(config: any): Array<string> {
    const arr: Array<string> = [];
    // if (config?.tablePanels) {
    //   config.tablePanels.forEach(tablePanel => {
    //     tablePanel.content.tableColumns.forEach(item => {
    //       arr.push(item.field);
    //     });
    //   });
    // } else
    if (config?.tableColumns) {
      config.tableColumns.forEach(item => {
        arr.push(item.field);
      });
    }

    return arr;
  }

  private getPmsForm(config: any): Array<string> {
    const arr: Array<string> = [];
    if (config?.formPanels) {
      config.formPanels.forEach(formPanel => {
        formPanel.content.formColumns.forEach(item => {
          arr.push(item.field);
        });
      });
    } else if (config?.formColumns) {
      config.formColumns.forEach(item => {
        arr.push(item.field);
      });
    }

    return arr;
  }

  private getPmsAddonData(config: T_basePanelConf): Array<string> {
    const arr: Array<string> = [];
    if (Array.isArray(config?.toolbar?.addon)) {
      config.toolbar.addon.forEach(ctm => {
        arr.push(ctm.title);
        if (ctm.children.length > 0) {
          // eslint-disable-next-line no-use-before-define
          handleAddonData(ctm.children);
        }
      });
    }

    function handleAddonData(children): void {
      for (const child of children) {
        arr.push(child.title);
        if (child.children.length > 0) {
          handleAddonData(child.children);
        }
      }
    }
    return arr;
  }
}
